package com.jinzhending.utils;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Arrays;

public class AESUtil {
    /**
     *
     * @author ngh
     * AES128 算法
     *
     * CBC 模式
     *
     * PKCS7Padding 填充模式
     *
     * CBC模式需要添加偏移量参数iv，必须16位
     * 密钥 sessionKey，必须16位
     *
     * 介于java 不支持PKCS7Padding，只支持PKCS5Padding 但是PKCS7Padding 和 PKCS5Padding 没有什么区别
     * 要实现在java端用PKCS7Padding填充，需要用到bouncycastle组件来实现
     */
    private final static String sessionKey = "shanxizhending,.";
    // 偏移量 16位
    private final static String iv = "1994022119940221";

    // 算法名称
    final static String KEY_ALGORITHM = "AES";
    // 加解密算法/模式/填充方式
    final static String algorithmStr = "AES/CBC/PKCS7Padding";
    // 加解密 密钥 16位

    static byte[] ivByte;
    static byte[] keybytes;
    private static Key key;
    private static Cipher cipher;
    boolean isInited = false;

    public static void init() {
        // 如果密钥不足16位，那么就补足.  这个if 中的内容很重要
        keybytes = sessionKey.getBytes();
        ivByte = iv.getBytes();
        int base = 16;
        if (keybytes.length % base != 0) {
            int groups = keybytes.length / base + (keybytes.length % base != 0 ? 1 : 0);
            byte[] temp = new byte[groups * base];
            Arrays.fill(temp, (byte) 0);
            System.arraycopy(keybytes, 0, temp, 0, keybytes.length);
            keybytes = temp;
        }
        // 初始化
        Security.addProvider(new BouncyCastleProvider());
        // 转化成JAVA的密钥格式
        key = new SecretKeySpec(keybytes, KEY_ALGORITHM);
        try {
            // 初始化cipher
            cipher = Cipher.getInstance(algorithmStr, "BC");
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 加密方法
     *
     * @param content
     *            要加密的字符串

     * @return
     */
    public static String encrypt(String content) {
        byte[] encryptedText = null;
        byte[] contentByte = content.getBytes();

        init();
        try {
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(ivByte));
            encryptedText = cipher.doFinal(contentByte);;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new String(Hex.encode(encryptedText));
    }
    /**
     * 解密方法
     *
     * @param encryptedData
     *            要解密的字符串

     * @return
     */
    public static String decrypt(String encryptedData) {
        byte[] encryptedText = null;
        byte[] encryptedDataByte = Hex.decode(encryptedData);
        init();
        try {
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ivByte));
            encryptedText = cipher.doFinal(encryptedDataByte);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new String(encryptedText);
    }

    public static void main(String[] args) {


        //加密字符串
        String content = "{\"id\":1,\"updateTime\":null,\"createTime\":null,\"username\":\"1\",\"password\":null,\"idCard\":null,\"accountMoney\":null,\"goodsInfoList\":[{\"id\":1,\"updateTime\":null,\"createTime\":null,\"goodsName\":\"测试数据1测试数据1测试数据1测试数据1测试数据1测试数据1测试数据1\",\"goodsCoverImageUrl\":\"/1.jpg\",\"goodsMoneyNumber\":\"1000\",\"goodsNumber\":0,\"goodsDetails\":null,\"goodsSell\":1,\"goodsUserId\":0,\"goodsSeeNumber\":9,\"goodsExpressMoney\":\"1\"},{\"id\":2,\"updateTime\":null,\"createTime\":null,\"goodsName\":\"1\",\"goodsCoverImageUrl\":\"/1.jpg\",\"goodsMoneyNumber\":\"1\",\"goodsNumber\":0,\"goodsDetails\":null,\"goodsSell\":1,\"goodsUserId\":0,\"goodsSeeNumber\":1,\"goodsExpressMoney\":\"5\"},{\"id\":3,\"updateTime\":null,\"createTime\":null,\"goodsName\":\"2\",\"goodsCoverImageUrl\":\"/1.jpg\",\"goodsMoneyNumber\":\"1\",\"goodsNumber\":0,\"goodsDetails\":null,\"goodsSell\":1,\"goodsUserId\":0,\"goodsSeeNumber\":1,\"goodsExpressMoney\":\"10\"},{\"id\":4,\"updateTime\":null,\"createTime\":null,\"goodsName\":\"1\",\"goodsCoverImageUrl\":\"/1.jpg\",\"goodsMoneyNumber\":\"1\",\"goodsNumber\":0,\"goodsDetails\":null,\"goodsSell\":1,\"goodsUserId\":0,\"goodsSeeNumber\":1,\"goodsExpressMoney\":\"0\"},{\"id\":5,\"updateTime\":null,\"createTime\":null,\"goodsName\":\"1\",\"goodsCoverImageUrl\":\"/1.jpg\",\"goodsMoneyNumber\":\"1\",\"goodsNumber\":0,\"goodsDetails\":null,\"goodsSell\":1,\"goodsUserId\":0,\"goodsSeeNumber\":1,\"goodsExpressMoney\":\"0\"},{\"id\":6,\"updateTime\":null,\"createTime\":null,\"goodsName\":\"1\",\"goodsCoverImageUrl\":\"/1.jpg\",\"goodsMoneyNumber\":\"1\",\"goodsNumber\":0,\"goodsDetails\":null,\"goodsSell\":1,\"goodsUserId\":0,\"goodsSeeNumber\":1,\"goodsExpressMoney\":\"0\"},{\"id\":7,\"updateTime\":null,\"createTime\":null,\"goodsName\":\"1\",\"goodsCoverImageUrl\":\"/1.jpg\",\"goodsMoneyNumber\":\"1\",\"goodsNumber\":0,\"goodsDetails\":null,\"goodsSell\":1,\"goodsUserId\":0,\"goodsSeeNumber\":1,\"goodsExpressMoney\":\"0\"},{\"id\":8,\"updateTime\":null,\"createTime\":null,\"goodsName\":\"1\",\"goodsCoverImageUrl\":\"/1.jpg\",\"goodsMoneyNumber\":\"1\",\"goodsNumber\":0,\"goodsDetails\":null,\"goodsSell\":1,\"goodsUserId\":0,\"goodsSeeNumber\":11,\"goodsExpressMoney\":\"0\"},{\"id\":10,\"updateTime\":null,\"createTime\":null,\"goodsName\":\"1\",\"goodsCoverImageUrl\":\"/1.jpg\",\"goodsMoneyNumber\":\"1\",\"goodsNumber\":0,\"goodsDetails\":null,\"goodsSell\":1,\"goodsUserId\":0,\"goodsSeeNumber\":1,\"goodsExpressMoney\":\"0\"},{\"id\":11,\"updateTime\":null,\"createTime\":null,\"goodsName\":\"1\",\"goodsCoverImageUrl\":\"/1.jpg\",\"goodsMoneyNumber\":\"1\",\"goodsNumber\":0,\"goodsDetails\":null,\"goodsSell\":1,\"goodsUserId\":0,\"goodsSeeNumber\":1,\"goodsExpressMoney\":\"0\"}]}";
        System.out.println("加密前的：" + content);
//        System.out.println("加密密钥：" + new String(keybytes));
        // 加密方法
        String enc = AESUtil.encrypt(content);
        System.out.println("加密后的内容：" + enc);

        // 解密方法
        String dec = AESUtil.decrypt(enc);
        System.out.println("解密后的内容：" + dec);
    }
}
//{"data":"c7b3aa3375e7fadb24aa24c4028e8969f8dd3a03de9062cfa17c2287c2ff03471b3022a46cdb6915926fad0b1a98c510b475bd6970fb60e9319b5cee052413c974381abcf4e20a4638327d7ab6ccbc0d06f953137219b6a5d4e3282695bdd2c8bccb58735c263da3c297fc41fe167be60ed50c67157fba46531a03ae02d1d7d6294ea2a82d8bfe52b134c97a82147d5722c752b3d82a1dbc024fc6025dc5e0067ec605e4109a48da99e27bc0030d9a7300b285e8d63e0ae2f8e153cbb3fb73c4552450ba2d8ed5d31820a29165ae84f4342fae5059987451135caa39875f4cec7eb555daba78f3d65ea72886375f6cfb4e44e5e60d3e47b252875d99efd8d755f15fafd26bb47eaf21ce00def998a1eadbaa6ad014353949dd277e3090644b4d7a55907be2b7588dde1f7b5f2bd7cecec9cbb83758e59ae29f381fb3a1e54e26b0db3f3c26e9fa0abff7603c302583c9607a502daf2e98de7bf78735eec12954fe5376f27e7e0ba148d78fdc6b8d3d80803d9c7041949c44201a1eddf04702923df5ecf6ede68fd9081dabc955961f74368c32e4fdf6057f80048cb1f59c1d6fa1597822b8a9dc53214be1411c1f7ce04fd62cd6f7c33361c9e9a5bf0d2fef4d5f788d44b5412d5cdc6cd3b9748864fd08104cbdc0b37e37367f2f1ce2185ac1432340d5b9d45172bc883fd39238dd36644c3b3ffac017af4a6648b4679497b37feb1a1d181eb68ebbf1211843e100078f389d4f1e65b0260bc66e90bdd8c615320b45df2cdaea3d470c1f8698f60ffc94f5e8f26ed379e134827f1f52bb254c3db56cc00723b4b9bd38ea74f753f627f6c405bf4c8427db0d8b8d59400531cf5069e6488a6a6a10ec054e2d079846968ec024ec1101f1772219c01d58cc64993f41ac24f1c6631324104e307e0e9b7e1c590f31bc18d469987fc4a78ca4ddf1bd7a815a424cfe2e6c4d07085218c36ea534fff5cd6b0723b5efaa9c40123c1b066400a03e6f09fd3c79bfc565a82e2acb3420dcd5fbe7324940c1ef7810ffa9c09645cfbcba20768b7861ce67762d6c8ef2604649ad3af7a65a72c8c1be51028d41cbbfde7c8c882ecddcbe01a8006b5ebdfc717107e4b3a3aa1b2762b5d0237d88e30efe76653ce037758127529a827fb3376d21e7c41c7f1a8ed7bdd96d9e5431f0ec133c457c17ce28381d7a9c3158db48c9cb9c8cd80fa22597e4914dab482ca2932a04c08e96924197a7d0afa213f2566c26609cf3f0cca69a40171ad6f53512b2e2d5be59b97eb56dda7e89b1b3de321a80261ee8e72f95ba3e180de04f1c291ffb9c9d67799528c81784553e6b825458b512ba60834ebe5efe85be291398d75a7f85086f9c236be227798d4e51163fab3c1e2ad35ab9a59737541ee00aa78d6c8a33727179b3a89ad066ab0aa576ef5b3940639fbf228eb5bed8b9181f3db4e2bc3564cd414f71401e9afefb82df59b5fe20d599e80369a7e25a1fdfd9d73946b1268cd3b523d43ec3e15e9249f5624d4be25bb27a3a3e4945a4f50739d01a95b9bf3b5636bb2262d3b3bc16c7b3d1fc12a6facb76c7fd6c8ad8623b3eed7c48ee5138160d3492111e9cc67841496b6a2625a055862f14de98b04f9324cf618f8b2ca009fcccfd3ecf32de81f5f09cca462324259e497583167a4e498d3fef51ccf618a5fc965f718adcdefaef22560e4ac73c682591b862152aafc92befb6aaad792cd190e17c5fcf8431021d1893fdd72243077e6abf415f729788baeae2295c46ddf42e7ef89670a98b14dee954250546ef0dc83d1de9a7fea0119b9b9bae8ba8c5d008ef2ed9991d6e5cf67cdc88afd907806dd6a491db85eb6e9fa830436ecb235ad0a99f3d91f0e17668331e877faa83b7c39a2bd95eed14698267daf9858d97a713e1c3ddc79e5d7c6adb26d0aec55ba5755aa4231f74273794f446f525cf33b20c5f46dc503d468d3977d91cb3b586d8fd76610c144afb46506cffebdbd9bb6e78611553723a9c3ca5d547c79fc67e7eb3806d0c0df60fff0d7f64ed8636026857f0e2747ac5acc7ee2b87596906367754b894bd52a7dafcc3c2295a6b91554318f96d166cbb43f5c6f22937196e936f7ba5c7fb4287f84772c03d5892941c07d5fa413d754bad4d4f4c9ee3291849b411c4783e9e2a57c00c4f7c0b4b50bb8a3ec13045b44e1dd2592f8f41ed0b40f111de48e2334b7b48f0efb857d438da78db161649a6d81cd8cd8045eb4bacc27f427498c38a9b20bb1f4b8f4d7f64f3f5b2a050c3400e75619121177a062a57e4eeda4cb9955d112ba8b60cb5dcaa63db432d21edd07e8c3dbe645a621f337d4090989d71355e984ac01d55240ebbbdeb2402fc81761e2bf23660a0547bde792a35652c5451901607485d67fe8387254e9d47c95c79bf5bafa6f5822c69f3046e1a1e9070bd199e742f9b47dd03bf92bd18d56eb09e96ba471449e6027c1a75afaf647592bc0a80c03b89c493d600404ba465aa8ea14aafa53ce43ae6793f8fd0a5c7c2e20239dd9eb6140277ff169029d499b907892955c2498edda4d6c302f3e1ad0ac77957a07e8a050409a8c89d08018000dc6bda763b87e3f32d3fe84156bd975649bab43e8fad7e117a333f8e76adfef5ba04b739f75c36028bccdc621372f77cb6ccff06ce2714ca84f63e268c14c916ceb5eeb98fa15ec2f04e3d32d6affbd18e829e38612ff308312878a366d30381f01e6fc06026b3c940d49456e0fed518d2d0135ab2bb13439af40557cb98d5e4adec3f646972bc32d5312a2b61fd37c0d718fcad5c71a756015425d83e420e2f9a0885596b3f5e21d3bda55e257b1e2d86f3282c50fc2efe0756cd95e04037d7bfd53c19c10f92c2d37ae083fcca43308b84a6566bbadf6832304b63768e05d070031758b9a96c10d5927cea8ed1cc77d93d621e502c2323d036c1aeca2cfd4cef73c91c86a65e11606b35cf5e81b13d09e97c59aceed5c4b1e1474e3b6d1bb5ebcfb6e81bfa1f84fd18873c909d58e4359b06a557962c7cebda598ca9a8cbb0ef4089de2a85bd6a84da17257e5a4e36334b8635bd8ce2311c271945c4efa18b2a103d6d4ea2d50dc1d0a0f3c9e7a5fa118ac546d139a944293bb7770e94f3970bd4a7e4a3452110a1f900103efb397f721638d9d546b27a625d48b6e674de66b6481b022853dff5af284ea252badcc159cea236a4feec0af413361a6b36268aa72180aa10908fb788dc553f5db6217b0daf1626a38d5ddb3a6e23e6223007a849345b2b61636e3e4247443dc0ea5d925a92de33c1237c899d31ccd5ee1022e27ceb633535223cd324621d84580bf95fa2e98db2ee2a0eebc0513561f4df1fe1e73e2f23054c982c3d49c07a77f9ca79a4c9014be221fbd2da69bef79f1ef68c05b80ba2b50536b1a574e29b948ecc0b0eba4df3b645cc123b79522de0b8a9e6a32349d68811cc82a7d4ef42f1b7794f132ff5da53"}